//# sourceURL=http://uMapApp/UI/Checkbox
define([
    "mhjs/mhjs"
], function (
    mhjs
) {

    function Checkbox(option) {

        var _this = this;
        _this._isChecked = false;
        _this._isEnabled = true;

        _this.ui = {
            icon : new mhjs.Div({"class": "icon"}),
            label: new mhjs.Div({"class": "label"})
        };

        mhjs.Div.call(_this, option);
        _this.addClass("checkbox");

        _this.on("click", function () {
            _this.toggle();
        });

        _this
            .append(_this.ui.icon)
            .append(_this.ui.label.hide())
        ;

    }

    var _class = Checkbox;
    mhjs.Inherit(_class, mhjs.Div);


    Object.defineProperties(_class.prototype, {
        "label"    : {
            get: function () {
                return this._label;
            },
            set: function (label) {
                var _this = this;
                _this._label = label;
                _this.ui.label.show();
                _this.ui.label.text(label);

            }
        },
        "isChecked": {
            get: function () {
                return this._isChecked;
            },
            set: function (isChecked) {
                var _this = this;
                isChecked = Boolean(isChecked);
                _this.toggleClass("checked", isChecked);
                _this._isChecked = isChecked;
                _this.trigger("change");
            }
        },
        "toggle"   : {
            value: function () {
                var _this = this;
                if (_this.isEnabled) {
                    _this.isChecked = !_this.isChecked;
                }
            }
        },
        "isEnabled": {
            get: function () {
                return this._isEnabled;
            },
            set: function (isEnabled) {
                var _this = this;
                isEnabled = Boolean(isEnabled);
                _this.toggleClass("disable", !isEnabled);
                _this._isEnabled = isEnabled;
            }
        },
        "bind"     : {
            value: function (obj, param, event, eventF) {
                var _this = this;

                if (obj && obj[param] !== undefined) {
                    _this.isChecked = obj[param];

                    event = event || "change";

                    obj.on(event, function () {
                        if (obj._delegate !== _this) {
                            if (eventF) {
                                _this._isChecked = eventF();
                                _this.toggleClass("checked", _this._isChecked);
                            } else {
                                _this._isChecked = obj[param];
                                _this.toggleClass("checked", _this._isChecked);
                            }
                        } else {
                            obj._delegate = undefined;
                        }
                    });

                    _this.on("change", function () {
                        obj._delegate = _this;
                        obj[param] = _this.isChecked;
                    });

                }

            }
        }
    });

    return _class;

});